//
//  ViewController.m
//  TestForRecord
//
//  Created by 张 同乐 on 15/5/12.
//  Copyright (c) 2015年 张 同乐. All rights reserved.
//

#import "ViewController.h"
#import "RecordAudio.h"

@interface ViewController () <AudioDelegate>{
    RecordAudio *_recordAodio;
    NSString *_cafAudioURL;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view, typically from a nib.
    _recordAodio = [[RecordAudio alloc] init];
    _recordAodio.delegate = self;
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(50, 100, 100, 30);
    [self.view addSubview:btn];
    [btn setTitle:@"按下 说话" forState:UIControlStateNormal];
    [btn setTitle:@"松开 结束" forState:UIControlStateHighlighted];
    [btn setBackgroundColor:[UIColor blackColor]];
    [btn addTarget:self action:@selector(startRecord) forControlEvents:UIControlEventTouchDown];
    [btn addTarget:self action:@selector(stopRecord) forControlEvents:UIControlEventTouchUpInside];
//    [btn addTarget:self action:@selector(cancelRecord) forControlEvents:UIControlEventTouchDragExit];
    
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    playBtn.frame = CGRectMake(50, 100 + 50, 100, 30);
    [self.view addSubview:playBtn];
    [playBtn setTitle:@"播放" forState:UIControlStateNormal];
    [playBtn setBackgroundColor:[UIColor blackColor]];
    [playBtn addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *pauseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    pauseBtn.frame = CGRectMake(50, 100 + 50*2, 100, 30);
    [self.view addSubview:pauseBtn];
    [pauseBtn setTitle:@"暂停录音" forState:UIControlStateNormal];
    [pauseBtn setBackgroundColor:[UIColor blackColor]];
    [pauseBtn addTarget:self action:@selector(pauseRecord) forControlEvents:UIControlEventTouchUpInside];

    
    
    NSString *str = @"aaaaa%bbbb/cccc";
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSData *base64Data = [data base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
}

- (void)startRecord {
    NSLog(@"开始录音");
    _cafAudioURL = nil;
    [_recordAodio startRecord];
}
- (void)stopRecord {
    NSLog(@"停止录音");
    _cafAudioURL = [_recordAodio stopRecord];
}
- (void)cancelRecord {
    [_recordAodio cancelRecord];
}
- (void)pauseRecord {
    NSLog(@"暂停录音");
    [_recordAodio pauseRecord];
}
- (void)play {
    if (_cafAudioURL) {
        [_recordAodio playWithAudioUrlStr:_cafAudioURL];
    }
}

- (void)recordResult:(RecordState)status andAudioUrlStr:(NSString *)audioUrlStr {
    if (status == RecordStateStop) {
        
    }
}
- (void)audioPlayResult:(AudioPlayState)status {
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
